package uk.ac.cam.juliet.zedcat.db;

import java.sql.Date;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;
import java.util.ListIterator;

public class MySQLTest {

	
	public static void main(String[] args) {
		//Connect to the server
		dbAnalysisDatabase db = null;
		try{
			db = new dbAnalysisDatabase("//localhost:1337/juliet");
			db.connect();
		}catch(SQLException e){
			System.err.println("An error occurred connecting...");
			e.printStackTrace();
			return;
		}catch(ClassNotFoundException e){
			System.err.println("Class not found error!");
			return;
		}
		
		
			
		try{
			List<Action> actions = db.getActions();
			ListIterator<Action> iter = actions.listIterator();
			Action currAction;
			while(iter.hasNext())
			{	
				currAction = iter.next();
				System.out.println("\nActionID: " + currAction.getActionID());
				List<ActionInstance> instances = currAction.getInstances();
				ListIterator<ActionInstance> institer = instances.listIterator();
				ActionInstance currInstance;
				while(institer.hasNext()){
					currInstance = institer.next();
					System.out.println("\tInstance @ " + currInstance.getStartDate() + " for " + currInstance.getDuration());
				}
				List<String> tags = currAction.getTagIDs();
				ListIterator<String> tagIter = tags.listIterator();
				if(tags.size() > 0)
				{
					System.out.println("Tags:");
					while(tagIter.hasNext())
					{
						String tagID = tagIter.next();
						System.out.println("\t" + tagID + ": " + db.tagForID(tagID));
					}
				}
				
			}
			System.out.println("Most recent action is: " + db.getMostRecentAction().getActionID());
			
			//Now lets test getting sightings:
			
			Calendar cal = Calendar.getInstance();
			cal.set(2010, 1, 22, 23, 28, 0);
			System.out.println("Sightings since " + new Timestamp(cal.getTime().getTime()));
			List<Sighting> sightingList = db.getSightingsSince(new Timestamp(cal.getTime().getTime()));
			if(sightingList == null){
				System.out.println(">>null<<");
				return;
			}
			ListIterator<Sighting> sightingIterator = sightingList.listIterator();
			Sighting currSighting;
			while(sightingIterator.hasNext()){
				currSighting = sightingIterator.next();
				System.out.println(currSighting.getTime());
			}
			
			//Now let's update add settings entry
			db.setSettingsEntryForKey("FAIL", "INSERTION WIN");
			
			System.out.println("The last processed sightingID was: " + db.getSettingsEntryForKey("Processed"));
			System.out.println("This had a timestamp of: " + db.getTimestamp(db.getSettingsEntryForKey("Processed")));
		
			//Add a stats record
			java.util.Date utilDate = new java.util.Date();
			Date currDate = new Date(utilDate.getTime());
			System.out.println("About to insert: " + 1 + " " + currDate + " 1 " + 10);
			db.addStatsRecord(1, currDate, "1", 10);
			
			System.out.println("Action name for ID 1: " + db.getActionName("1"));
			
		}catch(SQLException e1){
			System.err.println("Oh dear...");
			e1.printStackTrace();
		}
		finally{
			try{
				db.closeConnection();
			}catch(SQLException e){
				System.err.println("Error on close");
			}
		}
	}
}
